package top.aicats.blog.BlogManage.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import javax.swing.*;
import java.security.Key;
import java.util.Date;

/**
 * @author sjc
 * @version 1.0
 * @date 2020/1/6 22:26
 * @email 952491025@qq.com
 */
public final class JwtUtils {
    private static String JWTSECRET = "qwe@123.com";
    public static String CreateJwt(String userName) {
        JwtBuilder builder = Jwts.builder()
                            .setSubject(JWTSECRET)
                            .claim("userName", userName)
                            .setExpiration(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7))
                            .signWith(SignatureAlgorithm.HS256,JWTSECRET);
        return builder.compact();
    }

    public static Claims CheckJWT(String token) {
        try {
            final Claims claims = Jwts.parser()
                                    .setSigningKey(JWTSECRET)
                                    .parseClaimsJws(token)
                                    .getBody();
            if (claims.get("username") == null) {
                return claims;
            }
        } catch (Exception e) {
            throw e;
        }
        return null;
    }

}
